package org.jeecg.modules.zcgl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.modules.zcgl.entity.AssetFixedCategory;
import org.jeecg.modules.zcgl.mapper.AssetFixedCategoryMapper;
import org.jeecg.modules.zcgl.service.IAssetFixedCategoryService;
import org.jeecg.modules.zcgl.vo.AssetFixedCategoryTreeModel;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 固定资产分类
 * @Author: jeecg-boot
 * @Date:   2021-06-30
 * @Version: V1.0
 */
@Service
public class AssetFixedCategoryServiceImpl extends ServiceImpl<AssetFixedCategoryMapper, AssetFixedCategory> implements IAssetFixedCategoryService {

    @Override
    public List<AssetFixedCategoryTreeModel> queryTreeList(String currentCompanyId) {
        LambdaQueryWrapper<AssetFixedCategory> query = new LambdaQueryWrapper<AssetFixedCategory>();
        query.orderByAsc(AssetFixedCategory::getCreateTime);
        List<AssetFixedCategory> list = this.list(query);
        List<AssetFixedCategoryTreeModel> res = new ArrayList<>();
        if(list !=null && list.size()>0){
            List<AssetFixedCategoryTreeModel>  listResult = list.stream().map(p->{
                AssetFixedCategoryTreeModel model = new AssetFixedCategoryTreeModel(p);
                return model;
            }).collect(Collectors.toList());
            res = listResult.stream().filter(m -> "0".equals(m.getParentId())).peek(
                    (m) -> m.setChildren(getChildren(m, listResult))
            ).collect(Collectors.toList());

        }
        return res;
    }

    /**
     * 递归获取子节点
     *
     * @param m
     * @param menus
     * @return java.util.List<com.pfms.master.entity.SysMenu>
     * @author chao.zhang
     * @date 2020/10/20 17:57
     **/
    private List<AssetFixedCategoryTreeModel> getChildren(AssetFixedCategoryTreeModel m, List<AssetFixedCategoryTreeModel> menus) {
        // 子节点parentId = 父节点ID
        return menus.stream().filter(m1 -> m.getId().equals(m1.getParentId()))
                .peek(m1 -> m1.setChildren(getChildren(m1, menus))).collect(Collectors.toList());
    }

}
